/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {

    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-status"><option value="">审核状态</option><option value="1">待审核</option><option value="2">审核已通过</option><option value="3">审核未通过</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/teacher/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('#check-teacher-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.teacher-id').val(id);
    });
    $('#check-teacher-dialog').on('hide.bs.modal', function(){
        $(this).find('.teacher-id').val('');
    });

    $('#check-teacher-dialog .btn-save').on('click', function(){
        var that = $('#check-teacher-dialog');
        var data = {
            id: that.find('.teacher-id').val(),
            status: that.find('input[name="teacher-status"]:checked').val()
        };

        sendAjax('/v1/manager/check/teacher', 'post', {data:data}, function(json){
            $('#check-teacher-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#delete-teacher-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        $(this).find('.teacher-id').val(id);
        $(this).find('.teacher-realname').text(name);
    });
    $('#delete-teacher-dialog').on('hide.bs.modal', function(){
        $(this).find('.teacher-id').val('');
        $(this).find('.teacher-realname').text('');
    });

    $('#delete-teacher-dialog .btn-save').on('click', function(){
        var that = $('#delete-teacher-dialog');
        var id = that.find('.teacher-id').val();
        sendAjax('/v1/manager/delete/teacher', 'post', {id:id}, function(json){
            that.modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, toolbar) {
    $("#teacher-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.status = $('.select-status').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'rent_user_name',
            title: '教师姓名（工号）'
        }, {
            field: 'gender',
            title: '性别',
            formatter: function (value) {
                return value == 1 ? '男' : '女';
            }
        }, {
            field: 'join_age',
            title: '进校年份（工龄）'
        }, {
            field: 'education',
            title: '学历'
        }, {
            field: 'education_code',
            title: '学历',
            visible: false
        }, {
            field: 'professional_title_code',
            title: '职称',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '正高级';
                    case 2 : return '副高级';
                    case 3 : return '中级';
                    case 4 : return '初级';
                }
            }
        }, {
            field: 'updated_at',
            title: '提交时间'
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value) {
                switch (value) {
                    case 0 : return '<span class="text-default"><b>待审核</b></span>';
                    case 1 : return '<span class="text-success"><b>审核已通过</b></span>';
                    case 2 : return '<span class="text-warning"><b>审核未通过</b></span>';
                    default : return '<span class="text-default"><b>待审核</b></span>';
                }
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-teacher" href="/manager/teacher/view/'+row.id+'.html">详情</a>';
                action += '<a class="btn btn-xs btn-primary check-teacher" data-toggle="modal" data-target="#check-teacher-dialog" data-id="'+row.id+'" data-status="'+row.status+'">审核</a>';
                action += '<a class="btn btn-xs btn-danger delete-teacher" data-toggle="modal" data-target="#delete-teacher-dialog" data-id="'+row.id+'" data-name="'+row.rent_user_name+'">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('#teacher-table').bootstrapTable('refresh');
}